package com.ymatou.iapi.login.testcase;

import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import java.util.List;
import com.google.gson.JsonObject;
import com.ymatou.iapi.login.parameter.BindWeiXinUseroRequestDtoBean;
import com.ymatou.iapi.login.parameter.UnbindWeiXinBean;
import com.ymatou.iapi.login.service.BindWeiXinUseroRequestDtoCall;
import com.ymatou.iapi.login.service.UnbindWeiXinCall;
import com.ymatou.iapi.userregister.parameter.UserRegisterInfoRequestDtoBean;
import com.ymatou.iapi.userregister.service.UserRegisterInfoRequestDtoCall;
import com.ymatou.iapi.userservice.parameter.enums.AppSourceEnum;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.database.model.ThirdpartyUserbind;
import com.ymttest.database.sqlwapper.JUserWapper;

/**
*解绑全球优选
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_UnbindWeiXin{
	private static UnbindWeiXinBean unbindweixinBean;
	private static UnbindWeiXinCall unbindweixinCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("解绑全球优选");
	}

	@Before
	public void caseUp() {
		unbindweixinBean=new UnbindWeiXinBean();
		unbindweixinCall=new UnbindWeiXinCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(unbindweixinCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UnbindWeiXin_001() {
		Logger.start(true,"解绑全球优选GlobalWxApp,成功");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			
			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token=registerResult.get("Token").getAsString();
			Integer userId=registerResult.get("UserId").getAsInt();
			BindWeiXinUseroRequestDtoBean bean=JUserCallService.initBindWeiXinUseroRequestDtoBean(token);
			bean.getWeiXinUser().setRegSource(AppSourceEnum.GlobalWxApp);
			BindWeiXinUseroRequestDtoCall wx = JUserCallService.bindWeiXinUseroRequestDto(bean);
			JUserWapper JUserWapper=new JUserWapper();
			List<ThirdpartyUserbind> list = JUserWapper.selectthirdpartyuserbind(userId).stream().filter(tu->{return tu.getAppSource()==AppSourceEnum.GlobalWxApp.getCode();}).collect(Collectors.toList());
			Logger.verifyEquals(1, list.size(), "验证有绑定记录");
			unbindweixinBean.setUserId(userId);
			unbindweixinCall.setData(unbindweixinBean);
			unbindweixinCall.callService();	
			Logger.verifyEquals("200", unbindweixinCall.getString("code"), "验证code");
//			Logger.debug(AppSourceEnum.GlobalWxApp.getCode());
			list = JUserWapper.selectthirdpartyuserbind(userId).stream().filter(tu->{return tu.getAppSource()==AppSourceEnum.GlobalWxApp.getCode();}).collect(Collectors.toList());
			Logger.verifyEquals(0, list.size(), "验证没有绑定记录");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	@Ignore
	public void Tc_UnbindWeiXin_002() {
		Logger.start(true,"无绑定解绑失败");
		try {
			//nothing todo
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			
			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token=registerResult.get("Token").getAsString();
			Integer userId=registerResult.get("UserId").getAsInt();
			unbindweixinBean.setUserId(userId);
			unbindweixinCall.setData(unbindweixinBean);
			unbindweixinCall.callService();	
			Logger.verifyEquals("200", unbindweixinCall.getString("code"), "验证code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	@Ignore
	//无效案例
	public void Tc_UnbindWeiXin_003() {
		Logger.start(true,"重复绑解绑失败");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			
			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token=registerResult.get("Token").getAsString();
			Integer userId=registerResult.get("UserId").getAsInt();
			BindWeiXinUseroRequestDtoBean bean=JUserCallService.initBindWeiXinUseroRequestDtoBean(token);
			bean.getWeiXinUser().setRegSource(AppSourceEnum.GlobalWxApp);
			BindWeiXinUseroRequestDtoCall wx = JUserCallService.bindWeiXinUseroRequestDto(bean);
			JUserWapper JUserWapper=new JUserWapper();
			List<ThirdpartyUserbind> list = JUserWapper.selectthirdpartyuserbind(userId).stream().filter(tu->{return tu.getAppSource()==AppSourceEnum.GlobalWxApp.getCode();}).collect(Collectors.toList());
			Logger.verifyEquals(1, list.size(), "验证有绑定记录");
			unbindweixinBean.setUserId(userId);
			unbindweixinCall.setData(unbindweixinBean);
			unbindweixinCall.callService();	
			unbindweixinCall.callService();	
			Logger.verifyEquals("200", unbindweixinCall.getString("code"), "验证code");
//			Logger.debug(AppSourceEnum.GlobalWxApp.getCode());
			list = JUserWapper.selectthirdpartyuserbind(userId).stream().filter(tu->{return tu.getAppSource()==AppSourceEnum.GlobalWxApp.getCode();}).collect(Collectors.toList());
			Logger.verifyEquals(0, list.size(), "验证没有绑定记录");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_UnbindWeiXin_004() {
		Logger.start(true,"不输入用户id失败");
		try {
			unbindweixinCall.setData(unbindweixinBean);
			unbindweixinCall.callService();			
			Logger.verifyEquals("400", unbindweixinCall.getString("code"), "验证code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_UnbindWeiXin_005() {
		Logger.start(true,"解绑全球优选,成功,不影响码头app绑定");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			
			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token=registerResult.get("Token").getAsString();
			Integer userId=registerResult.get("UserId").getAsInt();
			BindWeiXinUseroRequestDtoBean bean=JUserCallService.initBindWeiXinUseroRequestDtoBean(token);
			bean.getWeiXinUser().setRegSource(AppSourceEnum.GlobalWxApp);
			 JUserCallService.bindWeiXinUseroRequestDto(bean);
			
			bean.getWeiXinUser().setRegSource(AppSourceEnum.App);
			JUserCallService.bindWeiXinUseroRequestDto(bean);
			
			JUserWapper JUserWapper=new JUserWapper();
			List<ThirdpartyUserbind> list = JUserWapper.selectthirdpartyuserbind(userId).stream().filter(tu->{return tu.getAppSource()==AppSourceEnum.GlobalWxApp.getCode();}).collect(Collectors.toList());
			Logger.verifyEquals(1, list.size(), "验证有绑定记录");
			list = JUserWapper.selectthirdpartyuserbind(userId).stream().filter(tu->{return tu.getAppSource()==AppSourceEnum.App.getCode();}).collect(Collectors.toList());
			Logger.verifyEquals(1, list.size(), "验证有绑定记录");
			unbindweixinBean.setUserId(userId);
			unbindweixinCall.setData(unbindweixinBean);
			unbindweixinCall.callService();	
			Logger.verifyEquals("200", unbindweixinCall.getString("code"), "验证code");
//			Logger.debug(AppSourceEnum.GlobalWxApp.getCode());
			list = JUserWapper.selectthirdpartyuserbind(userId).stream().filter(tu->{return tu.getAppSource()==AppSourceEnum.GlobalWxApp.getCode();}).collect(Collectors.toList());
			Logger.verifyEquals(0, list.size(), "验证没有绑定记录");	
			list = JUserWapper.selectthirdpartyuserbind(userId).stream().filter(tu->{return tu.getAppSource()==AppSourceEnum.App.getCode();}).collect(Collectors.toList());
			Logger.verifyEquals(1, list.size(), "验证有绑定记录");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UnbindWeiXin_006() {
		Logger.start(true,"解绑全球优选GlobalApp,成功");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			
			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token=registerResult.get("Token").getAsString();
			Integer userId=registerResult.get("UserId").getAsInt();
			BindWeiXinUseroRequestDtoBean bean=JUserCallService.initBindWeiXinUseroRequestDtoBean(token);
			bean.getWeiXinUser().setRegSource(AppSourceEnum.GlobalApp);
			BindWeiXinUseroRequestDtoCall wx = JUserCallService.bindWeiXinUseroRequestDto(bean);
			JUserWapper JUserWapper=new JUserWapper();
			List<ThirdpartyUserbind> list = JUserWapper.selectthirdpartyuserbind(userId).stream().filter(tu->{return tu.getAppSource()==AppSourceEnum.GlobalApp.getCode();}).collect(Collectors.toList());
			Logger.verifyEquals(1, list.size(), "验证有绑定记录");
			unbindweixinBean.setUserId(userId);
			unbindweixinCall.setData(unbindweixinBean);
			unbindweixinCall.callService();	
//			Logger.debug(AppSourceEnum.GlobalWxApp.getCode());
			list = JUserWapper.selectthirdpartyuserbind(userId).stream().filter(tu->{return tu.getAppSource()==AppSourceEnum.GlobalWxApp.getCode();}).collect(Collectors.toList());
			Logger.verifyEquals(0, list.size(), "验证没有绑定记录");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}